<template>
    <el-container>
        <el-header height="80px" style="padding: 10px;">
            <!-- 工具/搜索栏 -->
            <div style="padding: 5px; background: #F2F6FC; border-radius: 5px;">
                <div class="topbar">
                    <div class="left-panel">
                        <span style="font-size: 14px;" :style="{'color': (connectStatus?'green':'red')}">{{ conncetInfo }}</span>
                    </div>
                    <div class="right-panel">
                        <div class="right-panel-search">
                            <el-input v-model="searchForm.searchText" placeholder="搜索应用名称..." style="width: 200px;"></el-input>
                            <el-button type="primary" icon="el-icon-search" @click="getApps()"></el-button>
                            <el-button type="warning" icon="el-icon-refresh" @click="refresh()"></el-button>
                        </div>
                    </div>
                </div>
            </div>       
        </el-header>

        <el-main style="padding-left: 10px; padding-right: 10px;">
            <el-card shadow="always" body-style="padding: 0px; min-height: 500px;">
                <el-table
                ref="Table"
                :data="list"
                element-loading-text="加载数据中"
                v-loading="loading"
                :header-row-style="{height:45+'px'}"
                :row-style="{height:60+'px'}"
                style="width: 100%; min-height: 450px;"
                :header-cell-style="{background:'#F2F6FC'}">
                    <el-table-column prop="id"  label="应用名称" sortable></el-table-column>
                    <el-table-column prop="instanceCount"  label="实例数" ></el-table-column>
                    <el-table-column prop="upInstanceCount"  label="在线实例数" sortable></el-table-column>
                    <el-table-column prop="downInstanceCount"  label="下线实例数" ></el-table-column>
                    <el-table-column label="操作" width="130">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                style="color: #67C23A"
                                icon="el-icon-view"
                                @click="views(scope.$index, scope.row)">查看实例</el-button>   
                        </template>
                    </el-table-column>
                    <template #empty>
                        <el-empty :description="'查无数据'" :image-size="100"></el-empty>
                    </template>
                </el-table>
            </el-card>
            
        </el-main>

    </el-container>
</template>

<script>

export default {
    components: {

    },
    data() {
        return {
            searchForm: {
                searchText: ''
            },
            list: [],
            conncetInfo: '',
            connectStatus: true,
            loading: false,
            visibleViewsDialog: false,
            currentRow: {},
        }
    },
    mounted() {       
        // 初始化列表
        this.getApps()
    },
    methods: {
        /**
         * @title 获取列表
         */
        async getApps() {
            this.loading = true
            this.conncetInfo = '正在连接注册中心......'
            try {
                let response = await this.$API.regcenter.getApps(this.searchForm)   
                this.list = response.data
                this.conncetInfo = '注册中心连接成功'
                this.connectStatus = true;
            } catch(error) {
                this.conncetInfo = '注册中心连接失败，请检查注册中心状态'
                this.connectStatus = false;
            } finally {
                this.loading = false         
            }
        },

        refresh() {
            this.searchForm.searchText = ''
            this.getApps()
        },


        views(index, row) {
            this.$router.push({ path: '/registry/instances', query: { appId: row.id }})
        },

        closeViews() {
            this.visibleViewsDialog = false
            this.currentRow = {}
        },
       
    }
}
</script>

<style lang="scss" scoped>
.el-main {
    width: 100%;
    height: 100%;
    padding: 0px;
}
.topbar {
    height: 50px; 
    display: flex;
    justify-content:space-between;
    margin-bottom: 0px;

    .left-panel {
        display: flex;
        align-items: center;
    }
    .right-panel {
        display: flex;
        align-items: center;
    }

    .right-panel-search {display: flex;align-items: center;}

    .right-panel-search > * + * {margin-left:10px;}
}

</style>